﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;

namespace ManageTicket.Tools
{
    public partial class frmItem : frmBase
    {
        #region Contructor
        public frmItem(string type)
        {
            InitializeComponent();
            TYPE = type;
        }
        #endregion

        #region Variable
        ManageTicket.Library.Control.ExecuteControl exeCtrl;

        //permission
        bool bAdd = false;
        bool bEdit = false;
        bool bDelete = false;
        bool bCancel = false;
        bool bSave = false;
        bool bFunction = false;
        bool bView = false;
        bool bFind = false;
        bool bExport = false;
        bool bSendmail = false;
        bool bPrint = false;

        clsMethod4All objMethod = new clsMethod4All();
        ManageTicket.Library.Common.CommonFunction clsCommonFunc = new ManageTicket.Library.Common.CommonFunction();
        private Business.Item boItem = new Business.Item();
        private Business.Employee boEmployee = new Business.Employee();
        string sModel = ""; //Store AND or UPDATE status
        private const string ADD_MODEL = "ADD";
        private const string UPDATE_MODEL = "UPDATE";
        private string TYPE = "";
        private string TYPENAME = "";
        #endregion

        #region Event
        private void frmItem_Load(object sender, EventArgs e)
        {
            switch (TYPE)
            {
                case "EVENT":
                    TYPENAME = "Sự kiện";
                    //permission
                    clsCommonFunc.GetPermission(ref bAdd, ref bEdit, ref bDelete, ref bCancel, ref bSave, ref bFunction, ref bView, ref bFind, ref bExport, ref bSendmail, ref bPrint, frmMain.CurrentUser.UserId, "FORM_09");
                    break;
                case "REMIND":
                    TYPENAME = "Nhắc nhở";
                    //permission
                    clsCommonFunc.GetPermission(ref bAdd, ref bEdit, ref bDelete, ref bCancel, ref bSave, ref bFunction, ref bView, ref bFind, ref bExport, ref bSendmail, ref bPrint, frmMain.CurrentUser.UserId, "FORM_10");
                    break;
                case "SHARE":
                    TYPENAME = "Chia sẻ";
                    //permission
                    clsCommonFunc.GetPermission(ref bAdd, ref bEdit, ref bDelete, ref bCancel, ref bSave, ref bFunction, ref bView, ref bFind, ref bExport, ref bSendmail, ref bPrint, frmMain.CurrentUser.UserId, "FORM_11");
                    break;
            }

            this.Text = TYPENAME;

            //execute control
            exeCtrl = new ManageTicket.Library.Control.ExecuteControl();
            exeCtrl.Dock = DockStyle.Fill;
            exeCtrl.lblDescription.Text = "Cập nhật " + TYPENAME;
            pnlExe.Controls.Add(exeCtrl);

            objMethod.ClearText(new Control[] { txtID, txtEmployeeId, txtFullName, txtTitle, txtRemark });
            chkStatus.Checked = false;
            objMethod.en_dis_control(new Control[] { grpListItem }, new Control[] { grpDetail });
            LoadStatusOnce();
            LoadStatus(true, false, false, false, false);

            //default date value
            dtpDateFrom.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0);
            dtpDateTo.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, objMethod.GetEndDayOfMonth(DateTime.Now.Month, DateTime.Now.Year), 23, 59, 59);
        }

        private void lvwItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwItem.SelectedItems.Count > 0)
            {
                int id = int.Parse(lvwItem.SelectedItems[0].Tag.ToString());
                FillData(id);
                LoadStatus(true, true, true, false, false);
            }
            else
            {
                LoadStatus(true, false, false, false, false);
                objMethod.ClearText(new Control[] { txtID, txtEmployeeId, txtFullName, txtTitle, txtRemark });
            }
        }

        private void txtEmployeeId_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                DataObjects.Employee oCus = boEmployee.GetObjectByPK(txtEmployeeId.Text);
                if (oCus != null)
                {
                    txtFullName.Text = oCus.LastName + " " + oCus.FirstName;
                    txtFullName.Focus();
                }
            }
        }

        private void dtpDateFrom_ValueChanged(object sender, EventArgs e)
        {
            GetListItem(dtpDateFrom.Value, dtpDateTo.Value);
        }

        private void dtpDateTo_ValueChanged(object sender, EventArgs e)
        {
            GetListItem(dtpDateFrom.Value, dtpDateTo.Value);
        }
        #endregion

        #region Execute
        public override void Add()
        {
            objMethod.ClearText(new Control[] { txtID, txtEmployeeId, txtFullName, txtTitle, txtRemark }); 
            LoadStatus(false, false, false, true, true);
            objMethod.en_dis_control(new Control[] { grpDetail }, new Control[] { grpListItem });

            dtpDateInput.Value = DateTime.Now;
            txtID.Text = (boItem.GetMaxID() + 1).ToString();

            txtEmployeeId.Text = frmMain.CurrentUser.EmployeeId;
            DataObjects.Employee oCus = boEmployee.GetObjectByPK(txtEmployeeId.Text);
            if (oCus != null)
            {
                txtFullName.Text = oCus.LastName + " " + oCus.FirstName;
            }

            dtpDateInput.Focus();
            sModel = ADD_MODEL;
        }

        public override void Edit()
        {
            if (lvwItem.SelectedItems.Count <= 0)
                return;
            LoadStatus(false, false, false, true, true);
            objMethod.en_dis_control(new Control[] { grpDetail }, new Control[] { grpListItem });

            dtpDateInput.Focus();
            sModel = UPDATE_MODEL;
        }

        public override void Delete()
        {
            try
            {
                if (txtID.Text == "")
                    return;

                if (MessageBox.Show("Bạn có thực sự muốn xóa?", "Aquarius", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //Check Existing in table
                    //if (this.CheckExitToDelete(oDept.DEPT_CODE)) {
                    //    MessageBox.Show(".", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //    return;
                    //}

                    //delete
                    boItem.DeleteByPK(int.Parse(txtID.Text));

                    MessageBox.Show("Xóa thành công.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Cancel();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public override void Cancel()
        {
            objMethod.ClearText(new Control[] { txtID, txtEmployeeId, txtFullName, txtTitle, txtRemark });
            GetListItem(dtpDateFrom.Value, dtpDateTo.Value);
            if (lvwItem.Items.Count > 0)
            {
                lvwItem.Items[0].Selected = true;
            }
            else
            {
                LoadStatus(true, false, false, false, false);
            }
            objMethod.en_dis_control(new Control[] { grpListItem }, new Control[] { grpDetail });

            sModel = "";
        }

        public override void Save()
        {
            if (CheckValid())
            {
                try
                {
                    DataObjects.Item oItem = new DataObjects.Item();
                    oItem.Id = int.Parse(txtID.Text);
                    oItem.Type = TYPE;
                    oItem.DateInput = dtpDateInput.Value;
                    oItem.Title = txtTitle.Text;
                    oItem.Remark = txtRemark.Text;
                    oItem.EmployeeId = txtEmployeeId.Text;
                    oItem.Status = chkStatus.Checked;
                    

                    if (sModel == ADD_MODEL)
                    {
                        //insert
                        boItem.Insert(oItem);
                    }
                    else if (sModel == UPDATE_MODEL)
                    {
                        boItem.Update(oItem);
                    }
                    MessageBox.Show("Cập nhật thành công.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Cancel();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + Environment.NewLine + "Không thể cập nhật.", "Aquarius Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        #endregion

        #region Method

        private void LoadStatusOnce()
        {
            exeCtrl.btnView.Enabled = false;
            exeCtrl.btnExport.Enabled = false;
            exeCtrl.btnSendmail.Enabled = false;
            exeCtrl.btnPrint.Enabled = false;
            exeCtrl.btnFunction.Enabled = false;
            exeCtrl.btnFind.Enabled = false;

            exeCtrl.btnView.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnExport.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnSendmail.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnPrint.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnFunction.BackColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnFind.ButtonColor = Color.FromArgb(171, 171, 171);

            exeCtrl.btnView.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnExport.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnSendmail.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnPrint.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnFunction.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnFind.ForeColor = Color.FromArgb(241, 244, 251);
        }

        private void LoadStatus(bool blAdd, bool blEdit, bool blDelete, bool blCancel, bool blSave)
        {
            exeCtrl.btnAdd.Enabled = blAdd & bAdd;
            exeCtrl.btnEdit.Enabled = blEdit & bEdit;
            exeCtrl.btnDelete.Enabled = blDelete & bDelete;
            exeCtrl.btnCancel.Enabled = blCancel & bCancel;
            exeCtrl.btnSave.Enabled = blSave & bSave;

            exeCtrl.btnAdd.ButtonColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnEdit.ButtonColor = exeCtrl.btnEdit.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnDelete.ButtonColor = exeCtrl.btnDelete.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnCancel.ButtonColor = exeCtrl.btnCancel.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnSave.ButtonColor = exeCtrl.btnSave.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);

            exeCtrl.btnAdd.ForeColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnEdit.ForeColor = exeCtrl.btnEdit.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnDelete.ForeColor = exeCtrl.btnDelete.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnCancel.ForeColor = exeCtrl.btnCancel.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnSave.ForeColor = exeCtrl.btnSave.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
        }

        private void GetListItem(DateTime datefrom, DateTime dateto)
        {
            try
            {
                lvwItem.Items.Clear();
                ArrayList arr = boItem.GetListWithCondition(datefrom, dateto, TYPE, null);
                if (arr.Count > 0)
                {
                    ListViewItem item;
                    foreach (DataObjects.Item oItem in arr)
                    {
                        item = new ListViewItem();
                        item.Tag = oItem.Id.ToString();
                        item.Text = oItem.DateInput.Value.ToString("dd/MM/yyyy hh:mm tt");
                        DataObjects.Employee oCus = boEmployee.GetObjectByPK(oItem.EmployeeId);
                        if (oCus != null)
                            item.SubItems.Add(oCus.LastName + " " + oCus.FirstName);
                        else
                            item.SubItems.Add("");
                        item.SubItems.Add(oItem.Title);
                        item.SubItems.Add(oItem.Remark);
                        
                        lvwItem.Items.Add(item);
                    }

                    if (lvwItem.Items.Count > 0)
                    {
                        lvwItem.Items[0].Selected = true;
                    }
                }
                else
                {
                    objMethod.ClearText(new Control[] { txtID, txtEmployeeId, txtFullName, txtTitle, txtRemark });
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void FillData(int id)
        {
            DataObjects.Item oItem = boItem.GetObjectByPK(id);
            DataObjects.Employee oEmp;
            if (oItem != null)
            {
                txtID.Text = oItem.Id.ToString();
                dtpDateInput.Value = oItem.DateInput.Value;
                txtEmployeeId.Text = oItem.EmployeeId;
                oEmp = boEmployee.GetObjectByPK(oItem.EmployeeId);
                if (oEmp != null)
                    txtFullName.Text = oEmp.LastName + " " + oEmp.FirstName;
                txtTitle.Text = oItem.Title;
                txtRemark.Text = oItem.Remark;
                chkStatus.Checked = oItem.Status.Value;
            }
        }

        private bool CheckValid()
        {
            if (txtID.Text.Trim().Length == 0)
            {
                MessageBox.Show("Mã không được rỗng.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtID.Focus();
                return false;
            }

            if (txtTitle.Text.Trim().Length == 0)
            {
                MessageBox.Show("Vui lòng nhập tiêu đề.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTitle.Focus();
                return false;
            }

            if (txtEmployeeId.Text.Trim().Length == 0)
            {
                MessageBox.Show("Vui lòng chọn người lập.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmployeeId.Focus();
                return false;
            }
            else
            {
                DataObjects.Employee oEmp = boEmployee.GetObjectByPK(txtEmployeeId.Text);
                if (oEmp == null)
                {
                    MessageBox.Show("Nhân viên không tồn tại.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtEmployeeId.Focus();
                    return false;
                }
            }

            return true;
        }

        #endregion
        
    }
}
